package asia.yuanding.wechat.system.util;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.NumberUtil;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @ClassName: SystemUtil
 * @Author: YuanDing
 * @Date: 2025/7/26 15:35
 * @Description:
 */

@Component
@Data
public class SystemUtil {

    @Value("${system.code.expire}")
    private Integer codeExpire;

    @Value("${system.code.length}")
    private Integer codeLength;


    /**
     * 获取邮件验证码
     * @return
     */
    public Integer getCode() {
        String baseNumber = "1";
        for (int i = 0; i < codeLength; i++) {
            baseNumber += "0";
        }


        return  NumberUtil.generateRandomNumber(0, Integer.parseInt(baseNumber), 1)[0];
    }

    public static String getWxId(){
        return "wx_" + IdUtil.fastSimpleUUID().substring(0, 8);
    }


    public static Date getDateIfStr(Object dateObj) {

        if (dateObj == null)  return null;

        if (dateObj instanceof String) {
            String dateStr = dateObj.toString();
            long timestamp = (long) Double.parseDouble(dateStr);
            return DateUtil.date(timestamp);
        }

        return null;
    }



}
